home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 139 / c / flopram.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-05-13  |  11.0 KB  |  211 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Module:     flopram.c - Track copy a floppy into a RAMdisk               */
  4. /*                                                                          */
  5. /* Programmer: George R. Woodside                                           */
  6. /*                                                                          */
  7. /* Date:       February 22, 1987                                            */
  8. /*                                                                          */
  9. /****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <osbind.h>
  13.  
  14. #define  RMAGIC  0x1234543L
  15. #define  E_CHNG  -14L
  16. #define  OHEAD   8704
  17.  
  18. /****************************************************************************/
  19. /*                                                                          */
  20. /* Structure returned from a Dfree() call                                   */
  21. /*                                                                          */
  22. /****************************************************************************/
  23.  
  24. struct frees 
  25. {
  26.   long  sp_clust;                       /* number of free clusters          */
  27.   long  sp_totc;                        /* total number of clusters         */
  28.   long  sp_ssize;                       /* sector size in bytes             */
  29.   long  sp_csize;                       /* cluster size in sectors          */
  30. };
  31.  
  32. /****************************************************************************/
  33. /*                                                                          */
  34. /* Structure of a BIOS Parameter Block                                      */
  35. /*                                                                          */
  36. /****************************************************************************/
  37.  
  38. struct bpb 
  39. {
  40.   int       byt_sect;                   /* bytes per sector                 */
  41.   int       sec_clust;                  /* sectors per cluster              */
  42.   int       byt_clust;                  /* bytes per cluster                */
  43.   int       dir_len;                    /* directory length                 */
  44.   int       fat_size;                   /* file allocation table size       */
  45.   int       fat2_start;                 /* start of second copy of fat      */
  46.   int       clust_start;                /* start of data clusters           */
  47.   int       clusters;                   /* number of clusters               */
  48.   int       flags;                      /* parameter flags                  */
  49.   long      ram_start;                  /* RAMdisk only! RAM start address  */
  50.   long      ram_magic;                  /* RAMdisk only! Magic number       */
  51. };
  52.  
  53.   struct  bpb  *bpb_at;                 /* assign a pointer                 */
  54.   long    block[2176];                  /* overhead buffer                  */
  55.  
  56. /****************************************************************************/
  57. /*                                                                          */
  58. /* Main - Test for a drive designator.                                      */
  59. /*        If valid, be sure the contents of the floppy                      */
  60. /*        will fit into the active RAMdisk.                                 */
  61. /*        If they will, copy them.                                          */
  62. /*                                                                          */
  63. /****************************************************************************/
  64.  
  65. main(argc,argv)
  66. int    argc;
  67. char  *argv[];
  68. {
  69.  
  70.   register  int  drive = -1;            /* drive to read                    */
  71.   register  int  sectors;               /* number of sectors to read        */
  72.   register  int  limit;                 /* space on RAMdisk                 */
  73.   register  long start;                 /* save RAMdisk data start here     */
  74.   register  long *from;                 /* copy from address                */
  75.   register  long *to;                   /* copy to address                  */
  76.  
  77.   if(argc > 1)                          /* if a drive entered,              */
  78.     {
  79.       if( ( *argv[1] == 'a') || ( *argv[1] == 'A') ) /* if drive a,         */
  80.         drive = 0;                      /* set it                           */
  81.  
  82.       if( ( *argv[1] == 'b') || ( *argv[1] == 'B') ) /* if drive b,         */
  83.         drive = 1;                      /* set it                           */
  84.     }                                   /* end argument entered             */
  85.   else                                  /* if no argument entered,          */
  86.     {
  87.       printf("Which floppy drive (a or b)? \n"); /* prompt for drive        */
  88.       drive = Cconin();                 /* read a key                       */
  89.  
  90.       if( ( drive == 'a') || ( drive == 'A') ) /* if drive a,               */
  91.         drive = 0;                      /* set it                           */
  92.  
  93.       if( ( drive == 'b') || ( drive == 'B') ) /* if drive b,               */
  94.         drive = 1;                      /* set it                           */
  95.     }                                   /* end prompt for drive             */
  96.  
  97.   if( (drive != 0) && (drive != 1) )    /* if drive still not known,        */
  98.     {
  99.       printf("flopram: Invalid floppy drive.\n"); /* log an error           */
  100.       exit(1);                          /* and punt                         */
  101.     }
  102.  
  103.   bpb_at = (struct bpb *)Getbpb(12);    /* get RAMdisk BPB                  */
  104.  
  105.   if(bpb_at == (struct bpb *) NULL)     /* if no RAMdisk,                   */
  106.     {
  107.       printf("flopram: No active RAMdisk.\n"); /* log an error              */
  108.       exit(1);                          /* and punt                         */
  109.     }
  110.  
  111.   if(bpb_at->ram_magic != RMAGIC)       /* if wrong RAmdisk,                */
  112.     {
  113.       printf("flopram: Wrong RAMdisk running.\n"); /* log an error          */
  114.       exit(1);                          /* and punt                         */
  115.     }
  116.  
  117.   start = bpb_at->ram_start;            /* get data address                 */
  118.  
  119.   Getbpb(drive);                        /* insure drive is established      */
  120.  
  121.   get_some(drive,1,block,17);           /* read FATs and directory          */
  122.  
  123.   limit = all_space();                  /* get RAMdisk size                 */
  124.  
  125.   sectors = sp_used(block);             /* compute sectors used on floppy   */
  126.  
  127.   if(sectors > limit)                   /* if too much data,                */
  128.     {
  129.       printf("flopram: Too much data on disk.\n"); /* log an error          */
  130.       exit(1);                          /* and punt                         */
  131.     }
  132.   from = &block[0];                     /* copy data from here              */
  133.   to = (long *)(start + 512L);          /* copy data to here                */
  134.  
  135.   for(limit = 0; limit < OHEAD/sizeof(long); limit++)
  136.     *to++ = *from++;                    /* copy the overhead portion        */
  137.  
  138.   get_some(drive,18,(start + (18L*512L)),sectors); /* read the data         */
  139.  
  140.   start = Rwabs(0,0L,2,0,12);           /* mark media as changed            */
  141. }                                       /* end main                         */
  142.  
  143. /****************************************************************************/
  144. /*                                                                          */
  145. /* Compute the number of sectors on the RAMdisk                             */
  146. /*                                                                          */
  147. /****************************************************************************/
  148.  
  149. all_space()
  150. {
  151.   struct  frees  space;                 /* put parameters here              */
  152.   register  int  savail;                /* sesctors available               */
  153.  
  154.   Dfree(&space,13);                     /* get RAMdisk space structure      */
  155.   savail = space.sp_totc * space.sp_csize; /* compute sectors               */
  156.   return(savail);                       /* and return it                    */
  157. }
  158.  
  159. /****************************************************************************/
  160. /*                                                                          */
  161. /* Compute sectors used in data area of drive.                              */
  162. /* FATs are 12 bits on a floppy, or floppy compatible RAmdisk.              */
  163. /*                                                                          */
  164. /* Start at the end of the FAT table, and back up to the first              */
  165. /* non-zero int. This is the last FAT used. Convert ints to bits, and       */
  166. /* round up to insure a full FAT entry. Divide by 12 to get FATs, or        */
  167. /* clusters, in use. Multiply clusters * 2 for sectors.                     */
  168. /*                                                                          */
  169. /****************************************************************************/
  170.  
  171. sp_used(dstart)
  172. long   dstart;                          /* overhead start address           */
  173. {
  174.   register  int  *fatptr = (int *)(dstart + 1066L); /* end of FATs          */
  175.   register  int   i = 534;              /* ints in a FAT table + 1          */
  176.  
  177.   while(*fatptr-- == 0)                 /* until an active entry,           */
  178.     i--;                                /* count down the FATs              */
  179.  
  180.   i *= sizeof(int);                     /* convert to bytes                 */
  181.   i *= 8;                               /* convert to bits                  */
  182.   i += 11;                              /* round up for divide              */
  183.   i /= 12;                              /* number of FATs or clusters       */
  184.   i *= 2;                               /* convert FATs to sectors          */
  185.  
  186.   return(i-4);                          /* FATs 0 & 1 do not exist, so -4   */
  187. }
  188.  
  189. /****************************************************************************/
  190. /*                                                                          */
  191. /* Read a portion of the floppy.                                            */
  192. /* Using Rwabs eliminates track and sector counting, but requires           */
  193. /* that Mediach not be set. Mediach is checked before this call.            */
  194. /*                                                                          */
  195. /****************************************************************************/
  196.  
  197. get_some(drv,strt,buf,cnt)
  198. int   drv;                              /* drive to read                    */
  199. int   strt;                             /* starting sector number           */
  200. char  *buf;                             /* buffer address                   */
  201. int   cnt;                              /* sector count                     */
  202. {
  203.   register  long  reply;
  204.  
  205.   if((reply = Rwabs(0,buf,cnt,strt,drv) ) != 0L) /* if an error             */
  206.     {
  207.       printf("flopram: I/O error %D.\n"); /* log an error                   */
  208.       exit(1);                          /* and punt                         */
  209.     }
  210. }
  211.